package nl.tudelft.jpacman.e2e.framework.startup;

import io.cucumber.java.Before;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import nl.tudelft.jpacman.Launcher;
import nl.tudelft.jpacman.board.Direction;
import nl.tudelft.jpacman.board.Square;
import nl.tudelft.jpacman.board.Unit;
import nl.tudelft.jpacman.game.Game;
import nl.tudelft.jpacman.level.Pellet;
import nl.tudelft.jpacman.level.Player;

import javax.swing.*;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class MovePlayerSteps {
    private Launcher launcher;
    private Player player;
    private Square nextSquare;
    private Pellet pellet;
    private int score;
    private Square square;
    private BorderFactory borderFactory;

    private Game getGame() {
        return launcher.getGame();
    }

    @Before
    public void setup() {
        launcher = new Launcher();
        launcher.launch();
        launcher.withMapFile("simplemap.txt");
    }


    @Given("the game has started,and  my Pacman is next to a square containing a pellet;")
    public void theGameHasStartedAndMyPacmanIsNextToASquareContainingAPellet() {
        getGame().start();

        assertThat(getGame().isInProgress()).isTrue();
        List<Player> players = getGame().getPlayers();
        player = players.get(0);
        score = player.getScore();
        Square square = player.getSquare();

        nextSquare = square.getSquareAt(Direction.EAST);
        List<Unit> units = nextSquare.getOccupants();
        pellet = (Pellet) units.get(0);

        assertThat(units.size()).isEqualTo(1);
        assertThat(units.get(0)).isInstanceOf(Pellet.class);
    }

    @When("I press an arrow key towards that square;")
    public void i_press_an_arrow_key_towards_that_square() {
        getGame().move(player, Direction.EAST);
    }


    @Then("my Pacman can move to that square,and  I earn the points for the pellet,and  the pellet disappears from that square.")
    public void myPacmanCanMoveToThatSquareAndIEarnThePointsForThePelletAndThePelletDisappearsFromThatSquare() {
        assertThat(player.getSquare()).isEqualTo(nextSquare);
        assertThat(score).isEqualTo(0);
        assertThat(player.getScore()).isEqualTo(score + pellet.getValue());
        assertThat(nextSquare.getOccupants()).containsExactly(player).doesNotContain(pellet);
    }

    @Given("the game has started,and my Pacman is next to an empty square;")
    public void theGameHasStartedAndMyPacmanIsNextToAnEmptySquare() {
        getGame().start();

        assertThat(getGame().isInProgress()).isTrue();
        List<Player> players = getGame().getPlayers();
        player = players.get(0);
        score = player.getScore();
        Square square = player.getSquare();

        nextSquare = square.getSquareAt(Direction.EAST);
        List<Unit> units = nextSquare.getOccupants();

        assertThat(units.size()).isEqualTo(1);
    }

    @Then("my Pacman can move to that square and my points remain the same.")
    public void myPacmanCanMoveToThatSquareAndMyPointsRemainTheSame() {
        assertThat(player.getSquare()).isEqualTo(nextSquare);
        assertThat(score).isEqualTo(0);
        assertThat(nextSquare.getOccupants()).containsExactly(player);
    }

    @Given("the game has started,and my Pacman is next to a cell containing a wall;")
    public void theGameHasStartedAndMyPacmanIsNextToACellContainingAWall() {
        getGame().start();

        assertThat(getGame().isInProgress()).isTrue();
        List<Player> players = getGame().getPlayers();
        player = players.get(0);
        score = player.getScore();
        square = player.getSquare();

        nextSquare = square.getSquareAt(Direction.EAST);
        List<Unit> units = nextSquare.getOccupants();

        assertThat(units.size()).isEqualTo(1);
    }


    @When("I press an arrow key towards that cell;")
    public void iPressAnArrowKeyTowardsThatCell() {
        getGame().move(player, Direction.EAST);
    }

    @Then("the move is not conducted.")
    public void theMoveIsNotConducted() {
        assertThat(player.getSquare()).isEqualTo(nextSquare);
    }

//    @Given("the game has started,and my Pacman is next to a cell containing a ghost;")
//    public void theGameHasStartedAndMyPacmanIsNextToACellContainingAGhost() {
//    }
//
//    @Then("my Pacman dies,and  the game is over.")
//    public void myPacmanDiesAndTheGameIsOver() {
//    }
//
//    @When("I have eaten the last pellet;")
//    public void iHaveEatenTheLastPellet() {
//    }
//
//    @Then("I win the game.")
//    public void iWinTheGame() {
//    }
}
